<?php
/**
 * 
 * @author 彭荣贵
 *
 */

require_once './model/GetBookBaseInfo.php';
require_once 'sys_fns.php';
class GetAmazonBookInfo extends GetBookBaseInfo
{
	
	function __construct($url,$web_url,$search_key=NULL)
	{
		parent::__construct($url,$web_url,$search_key);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see GetBookBaseInfo::getBooks()
	 */
	function getBooks()
	{
		if(!isset($this->search_key)) //没有关键字搜索
		{
			$this->getIndexPageContent();
			$this->pagecontent = abandonBlank($this->pagecontent);
			$this->getIndexHotSaleBooks();
			$this->getIndexNewBooks();
			//var_dump($this->book_list);
			//exit();
		}
		else
		{
			
		}
		return $this->book_list;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see GetBookBaseInfo::getSearchPageContent()
	 */
	function getSearchPageContent()
	{
		
	}
	
	/**
	 * (non-PHPdoc)
	 * @see GetBookBaseInfo::setBookFromTag()
	 */
	function setBookFromTag(BookBaseEntity $book)
	{
		$book->_FROMTAG = BookBaseEntity::AMAZON;
	}
	
	/**
	 * 获取Amazon 首页中的书
	 */
	function getIndexBooks($pattern)
	{
		$books = array();
		$book  = array();
		preg_match($pattern, $this->pagecontent,$books);	
		
		if(!isset($books[1]))return;
		
		for($i=1;$i<6;$i++)
		{
			$book_base_entity = new BookBaseEntity();
			$j=$i+1;
			$book_pattern = '/s9a'.$i.'(.*)s9a'.$j.'/i';
			preg_match($book_pattern, $books[1],$book);
			$this->setIndexBookInfo($book_base_entity,$book[1]);
		}
	}
	
	/**
	 * 提取图书具体信息，书名 作者....
	 * @param BookBaseEntity $book
	 * @param string $book_string
	 */
	function setIndexBookInfo(BookBaseEntity $book,$book_string=null)
	{
		if(!isset($book_string))
			return;
		$this->setBookWebUrl($book); //设定网站主页 
		$this->setBookFromTag($book);
		$this->setBookFrom($book, 'Amazon');
		//file_put_contents('hot.txt', $book_string);
			
		$res = array();
		/*获取书名*/
		$pattern='/title="(((?!").)*)/i';
		preg_match($pattern, $book_string,$res);
		$this->setBookName($book, $res[1]);
			
		/*imgUrl*/
		$pattern='/src="(.*)"alt/i';
		preg_match($pattern, $book_string,$res);
			
		//var_dump($res[1]);
		if(strpos( $res[1],'url="'))
		{
				
			$pattern='/url="(.*)/i';
			preg_match($pattern, $res[1],$res);
		}
		$this->setBookImgUrl($book, $res[1]);
			
		/*detail url*/
		$pattern='/<ahref="(.*)"class/i';
		preg_match($pattern, $book_string,$res);
		$this->setBookDetailUrl($book, $book->_WEB_URL.$res[1]);
			
			
		/*author*/
		$pattern='/class="t12">(.*)<divclass="gryt12/i';
		preg_match($pattern, $book_string,$res);
		$res[1] = str_replace('</div>', '', $res[1]);
		$this->setBookAuthor($book, $res[1]);
			 
			
		/*commentUrl*/
		$pattern = '/noLinkDecoration"href="(.*)<spanclass="s9Long"/i';
		preg_match($pattern, $book_string,$res);
		$pattern = '/(.*)">(.*)/i';
		$comment =array();
		preg_match($pattern, $res[1],$comment);
		$this->setBookCommentUrl($book, $this->web_url.$comment[1]);
		/*comment nums*/
		$this->setBookCommentNums($book, $comment[2]);
			
			
		/*grade 总体评分*/
		$pattern='/s9Starss9Stars_(.*)<aclass="noLinkDecoration/i';
		preg_match($pattern, $book_string,$res);
		$res[1] = substr($res[1], 0,3);
		$res[1] = str_replace('_', '.', $res[1]);
		$this->setBookGrade($book, $res[1]);
			
			
		/*平装*/
		$pattern='/gryt12nt">(.*)<divclass="s9Customer/i';
		preg_match($pattern, $book_string,$res);
		$res[1]=str_replace('</div>', '', $res[1]);
		$this->setBookPingzhuang($book, $res[1]);
			
		/*价格*/
		$pattern='/newListpricegryt11">(.*)/i';
		preg_match($pattern, $book_string,$res);
		$price  = substr($res[1], 0,strpos($res[1], '<')-1); 
		$this->setBookPrice($book, $price);
			
		//discount price
		$pattern='/s9Priceredt14">(.*)/i';
		preg_match($pattern, $res[1],$res);
		$price  = substr($res[1], 0,strpos($res[1], '<')-1);
		$this->setBookDiscountPrice($book, $price);
		
		//json
		$this->bookToJson($book);
		
		//添加之前   json
		$this->addBookToList($book);
	}

	/**
	 * 获取首页热卖书
	 */
	function getIndexHotSaleBooks()
	{
		$pattern = '/热销图书推荐(.*)精选图书推荐/i';
		$this->getIndexBooks($pattern);
	}
		
	/**
	 * 获取首页中的新品热卖
	 */
	function getIndexNewBooks()
	{
		$pattern='/新品热卖(.*)热销图书推荐/i';
		$this->getIndexBooks($pattern);	
	}
	
}
?>